package cn.xz.kjg.adapter

import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import cn.xz.kjg.R
import cn.xz.kjg.bean.LightList
import cn.xz.kjg.net.ControlDevice
import cn.xz.kjg.util.AppHelper
import cn.xz.kjg.widgets.AutoScaleTextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * 灯光列表适配器*/
class LightCAdapter: BaseQuickAdapter<LightList, BaseViewHolder>(R.layout.item_light_c){

    override fun convert(holder: BaseViewHolder, item: LightList) {

        val tvLightC = holder.getView<AutoScaleTextView>(R.id.tv_light_c)
        tvLightC.post {
            tvLightC.text = item.name.toString()
        }

        holder.itemView.setOnClickListener {
            if (item.opening){
                //当前是开的，那么关掉
                ControlDevice.closeLight(item)
                item.opening = false
                notifyItemChanged(holder.adapterPosition)
            }else{
                ControlDevice.openLight(item)
                item.opening = true
                notifyItemChanged(holder.adapterPosition)
            }
        }

        val ivItemImg = holder.getView<ImageView>(R.id.iv_item_img)
        //是否已经开启
        ivItemImg.isSelected = item.opening

        val mStatus = holder.getView<ImageView>(R.id.iv_status)
        var mD = AppHelper.getDrawable(R.mipmap.icon_c_l)
        if (item.opening){
            mD = AppHelper.getDrawable(R.mipmap.icon_o_l)
        }
        mStatus.setImageDrawable(mD)
    }
}